package com.hc.java.io;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author huangchao E-mail:fengquan8866@163.com
 * @version 创建时间：2024/6/13 14:45
 */
public class AioClientDemo {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        try (
                AsynchronousSocketChannel client = AsynchronousSocketChannel.open()
        ) {
            Future<Void> localhost = client.connect(new InetSocketAddress("localhost", 9999));
            localhost.get();
            while (true) {
                Scanner scanner = new Scanner(System.in);
//        client.write(ByteBuffer.wrap("just a test".getBytes())).get();
                Future<Integer> write = client.write(ByteBuffer.wrap(scanner.nextLine().trim().getBytes()));
                System.out.println("已发送，我还要干自己的事！");
                write.get();
            }
        }
    }
}
